package com.example.lib_base.base.web

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import androidx.databinding.ViewDataBinding
import com.example.lib_base.R
import com.example.lib_base.base.fragment.BaseAppFragment
import com.example.lib_base.base.vm.BaseViewModel
import com.just.agentweb.*


abstract class BaseAgentWebFragment<B : ViewDataBinding, VM : BaseViewModel> :
    BaseAppFragment<B, VM>(){

    protected var mAgentWeb: AgentWeb? = null


     val mErrorLayoutEntity:ErrorLayoutEntity by lazy {
        ErrorLayoutEntity()
    }

    val mMiddleWareWebChrome: MiddlewareWebChromeBase by lazy {
        object :MiddlewareWebChromeBase(){
            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                setTitle(view,title)
            }
        }
    }

    val mMiddleWareWebClient: MiddlewareWebClientBase by lazy {
        object :MiddlewareWebClientBase(){

        }
    }

    protected abstract fun getAgentWebParent():ViewGroup
    protected abstract fun getUrl():String


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(getAgentWebParent(),ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT))
            .useDefaultIndicator(getIndicatorColor(),getIndicatorHeight())
            .setWebView(getWebView())
//            .setWebLayout()
            .setAgentWebWebSettings(getAgentWebSettings())
            .setWebViewClient(getWebViewClient())
            .setPermissionInterceptor(getPermissionInterceptor())
            .setWebChromeClient(getWebChromeClient())
            .interceptUnkownUrl()
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .setAgentWebUIController(getAgentWebUIController())
            .setMainFrameErrorView(mErrorLayoutEntity.layoutRes, mErrorLayoutEntity.reloadId)
            .useMiddlewareWebChrome(mMiddleWareWebChrome)
            .useMiddlewareWebClient(mMiddleWareWebClient)
            .createAgentWeb()
            .ready()
            .go(getUrl())
    }

   open fun setTitle(view:WebView?,title:String?){
    }

   protected open fun getIndicatorColor():Int{
        return -1
    }

   protected open fun getIndicatorHeight():Int{
        return -1
    }

    protected open fun getWebView():WebView?{
        return null
    }

    protected open fun getAgentWebSettings(): IAgentWebSettings<*>?{
       return AgentWebSettingsImpl.getInstance()
    }

    protected open fun getWebViewClient(): WebViewClient? {
       return null
    }

    protected open fun getPermissionInterceptor(): PermissionInterceptor? {
       return null
    }

    protected open fun getWebChromeClient(): WebChromeClient? {
       return WebChromeClient()
    }

    protected open fun getAgentWebUIController(): AgentWebUIControllerImplBase? {
       return null
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onDestroy() {
        mAgentWeb?.webLifeCycle?.onDestroy()
        super.onDestroy()
    }

    class ErrorLayoutEntity {
//        var layoutRes = R.layout.agentweb_error_page
        var layoutRes = com.just.agentweb.R.layout.agentweb_error_page

             set(value) {
                 field = if (value <= 0){
                     -1
                 }else{
                     value
                 }
             }
        var reloadId = 0
        set(value) {
            field = if (value <= 0){
                -1
            }else{
                value
            }
        }
    }
}